package com.cryann.mqttserver.modle;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author cRyann
 * @date 2018/12/6 0006 15:34
 * @description:
 */
@Data
@Slf4j
public class AccessorStore {

    private volatile static AccessorStore contextPool = null;
    private Map<String, Accessor> map = new ConcurrentHashMap<>();
    public static int CHECK_LIVE_INTERVAL_TIME = 20;

    private AccessorStore() {
    }

    public static AccessorStore instance() {
        if ( contextPool == null ) {
            contextPool = new AccessorStore();
        }
        return contextPool;
    }

    public void addContext(Accessor accessor) {
        if ( accessor != null ) {
            log.debug("AddOrUpdate Accessor [{}]", accessor.identifier);
            map.put(accessor.identifier, accessor);
        }
    }

    public void delContext(Accessor accessor) {
        if ( accessor != null ) {
            log.debug("Del Accessor [{}]", accessor.identifier);
            map.remove(accessor.identifier);
        }
    }

    public void delContext(String key) {
        map.remove(key);
    }

    public Accessor getContext(String key) {
        return map.get(key);
    }

}
